package com.tuling.llmlangchain4j.tools;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuling.llmlangchain4j.apointment.model.Appointment;
import com.tuling.llmlangchain4j.apointment.service.IAppointmentService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class AppointmentTools {
    @Autowired
    private IAppointmentService iAppointmentService;
    @Tool(name = "预约挂号",value = "根据参数先执行工具方法，queryDepartment 是否可以预约，并直接返回客户是否可以预约，")
    public String bookAppointment(Appointment apointment) {
        System.out.println("预约挂号APP 助手");
        //查询数据库是否包含预约记录
        Appointment appointmentDB = iAppointmentService.getOne(Wrappers.lambdaQuery(apointment));
        System.out.println("预约xxxxxxxxxxx");
        if(appointmentDB == null){
            System.out.println("预约yyyyyyyyyyyyyy");
            apointment.setId(null);
            iAppointmentService.save(apointment);
            return "预约成功,并返回预约详情";
        }else{
            return "预约失败，您在相同的科室和时间已有预约";
        }
    }


    @Tool(name = "取消挂号",value = "根据参数查询预约是不是存在，存在直接进行删除，返回true,否则返回false")
    public boolean cancelAppointment(Appointment apointment) {
        System.out.println("取消挂号APP 助手");
        //查询数据库是否包含预约记录
        Appointment appointmentDB = iAppointmentService.getOne(Wrappers.query(apointment));
        if(appointmentDB != null){
            System.out.println("取消");
            iAppointmentService.remove(Wrappers.query(appointmentDB));
            return true;
        }else{
            return false;
        }
    }

    @Tool(name = "查询是否有号源",value = "根据科室名称、日期、时间和医生查询是否有号源，并返回给用户")
    public boolean queryDepartment(
            @P(value = "科室名称")String name,
            @P(value = "日期")String date,
            @P(value = "时间 可选值 上午、下午")String time,
            @P(value = "医生名称",required = false)String doctorName
            ) {
        System.out.println("查询是否有号源");
        System.out.println("科室名称："+name);
        System.out.println("日期："+date);
        System.out.println("时间 ："+time);
        System.out.println("医生名称："+doctorName);

        //TODO
        //如果没有指定医生，查看排班医生，没有排班 返回false
        //如果有排班，查看医生排班是不是已经排满，如果排满返回false
        return true;
    }
}
